package com.yuan.util.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * 文本读取工具类
 * @author YouYuan
 * @date 2020/4/27 0:32
 */
public class ReadText {

    /**
     * 根据给定的编码读取文件为字符串
     * @param file
     * @param encoding
     * @return
     * @throws Exception
     */
    public static String readString(File file, String encoding) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
        StringBuilder buff = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            buff.append(line).append("\r\n");
        }
        br.close();
        return buff.toString();
    }
}
